package com.whz.governance.http.feign;

import com.whz.generic.Constants;
import lombok.experimental.UtilityClass;
import org.apache.http.HttpHeaders;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * some headers maybe access into next service with feign client
 *
 * @author whz
 */
@UtilityClass
public final class HttpAccessUtils {

    public static final List<String> REMOTE_IP_HEADERS = Arrays.asList(
            "x-forwarded-for",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_CLIENT_IP",
            "X-Real-IP");

    private static final List<String> HEADER_NAMES = new ArrayList<>(REMOTE_IP_HEADERS);

    static {
        HEADER_NAMES.add(Constants.X_USER_ID);
        HEADER_NAMES.add(Constants.X_USER_NAME);
        HEADER_NAMES.add(Constants.X_COMPANY_ID);
        HEADER_NAMES.add(Constants.X_DEPARTMENT_ID);
        HEADER_NAMES.add(Constants.X_DEPARTMENT_NAME);
        HEADER_NAMES.add(Constants.X_PRODUCT_ID);
        HEADER_NAMES.add(Constants.TOKEN);
        HEADER_NAMES.add(Constants.GREY_HEAD_KEY);
        // 透传认证请求头
        HEADER_NAMES.add(HttpHeaders.AUTHORIZATION);
    }

    public static void addHeaderName(String name) {
        HEADER_NAMES.add(Objects.requireNonNull(name));
    }

    /**
     * obtain headers those need to go through feign client
     */
    public static List<String> getAccessHeaders() {
        return Collections.unmodifiableList(HEADER_NAMES);
    }
}